'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
    // 注册
    async add() {
        let { ctx, app } = this;

        let { username, userpwd, userage, useraddress, usernumber  } = this.ctx.request.body;
		
		// 参数验证
		const paramVerification = await ctx.parameter({ username, userpwd, userage, useraddress, usernumber  })
		if(paramVerification !== ''){
		    return ctx.apiFail(paramVerification);
		}

   
		// 判断当前昵称是否存在
		const resSelect2 = await app.mysql.get('Client', { usernumber });
		if(resSelect2 !== null){
		    return ctx.apiFail('注册失败 该手机号码已存在');
		}

        // 数据库插入
        const result = await app.mysql.insert('Client', { username,userpwd,usernumber,useraddress,usersex:'',userage,userxueli:'',usercity:'',userzhanghao:''});

        if(result.affectedRows !== 1){
            return ctx.apiFail('注册失败');
        }

        return ctx.apiSuccess('注册成功');
    }
	
	// 登录
	async login() {
	    let { ctx, app } = this;
	
	    let { userpwd, usernumber  } = this.ctx.request.body;
		
		// 参数验证
		const paramVerification = await ctx.parameter({ userpwd, usernumber  })
		if(paramVerification !== ''){
		    return ctx.apiFail(paramVerification);
		}
		
		// 判断当前昵称是否存在
		const resSelect2 = await app.mysql.get('Client', { userpwd, usernumber });
		if(resSelect2 !== null){
			return ctx.apiSuccess(resSelect2, '登录成功');
		}
	    
		return ctx.apiFail('登录失败，账户或密码有误');
	}
	
	
	// 信息查询
	async dataList(){
		const { ctx, app } = this;
		
		const { pages, pageNumber } = ctx.query;
		
		// 参数验证
		const paramVerification = await ctx.parameter({ pages, pageNumber })
		if(paramVerification !== ''){
		    return ctx.apiFail(paramVerification);
		}
		
		// 查询数据总记录数
		const resultsCount = await this.app.mysql.query('SELECT count(*) FROM Client');
		const sum = resultsCount[0]['count(*)']; // 总页数
		
		// 分页查询数据
		const res = await this.app.mysql.select('Client', { 
		  limit: pages*1, // 每页数据量
		  offset: pageNumber*1, // 页码
		});
		
		const data = {
			list:res, // 数据
			sum, // 总记录数
			SumPages: Math.ceil(sum/pages*1), // 总页数
			pageNumber: pageNumber*1, // 当前页码 
		}
		
		return ctx.apiSuccess(data);
		
	}
	
	// 修改
	async update(){
		const { ctx, app } = this;
		let { userId, username, usernumber,useraddress,usersex,userage,userxueli, usercity, userzhanghao } = ctx.request.body;
		
		// 参数验证
		const paramVerification = await ctx.parameter({ userId, username, usernumber, })
		if(paramVerification !== ''){
		    return ctx.apiFail(paramVerification);
		}
		
		// 获取当前时间戳
		const created_at = (new Date()).getTime();
		
		// 要修改的值
		const row = {
		  username, usernumber,useraddress,usersex,userage,userxueli, usercity, userzhanghao
		};
		
		// 修改条件
		const options = {
		  where: {
		    userId
		  }
		};
		// 修改
		const result = await this.app.mysql.update('Client', row, options);
		
		// 判断是否成功
		if(result.affectedRows !== 1){
			return ctx.apiFail('更改失败');
		}
		return ctx.apiSuccess('更改成功');
	}
	
	// 删除
	async del(){
		const { ctx, app } = this;
		let { userId } = ctx.request.body;
		// 参数验证
		const paramVerification = await ctx.parameter({ userId })
		if(paramVerification !== ''){
		    return ctx.apiFail(paramVerification);
		}
		
		// 删除
		const result = await this.app.mysql.delete('Client', { userId });
		
		// 判断是否成功
		if(result.affectedRows !== 1){
			return ctx.apiFail('删除失败');
		}
		
		return ctx.apiSuccess('删除成功');
	}
}

module.exports = HomeController;
